<?php

namespace App\Http\Controllers\Develop;
use DB;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\User\Module;
use App\Models\User\ModuleStyle;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class ModuleController extends Controller
{
    public function index()
    {
        return view ('develop/moduleList');
    }

    //列表
    public function search(Request $request)
    {
        $where = [];
        $module_name = $request->input('module_name');
        $module_type = $request->input('module_type');

        if (!empty($module_name)) {
            $where[] = ['module_name',$module_name];
        }
        if (!empty($module_type)) {
            $where[] = ['module_type',$module_type];
        }

        $module_data = Module::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $module_data['total'],
            'rows' => []
        ];

        if ( $module_data['data'] ) {

            foreach( $module_data['data'] as $row ){

                if ($row['module_type'] == 1){
                    $module_type = '商品';
                }else{
                    $module_type = '图片';
                }

                $operation ='<a href="javascript: void(0);" onclick="Module.edit(' . $row['id'] . ')">修改</a>&nbsp;&nbsp;
                             <a href="javascript: void(0);" onclick="Module.del(' . $row['id'] . ')">删除</a>&nbsp;&nbsp;
                             <a href="javascript: void(0);" onclick="Style.edit(' . $row['id'] . ',1)">添加样式库 </a>&nbsp;&nbsp;
                             <a href="javascript: void(0);" onclick="Module.detail(' . $row['id'] . ')">查看样式库 </a>&nbsp;&nbsp;';

                $return['rows'][] = [
                    'operation' => $operation,
                    'module_name' => $row['module_name'],
                    'module_type' => $module_type
                ];
            }
        }

        return response()->json($return);

    }

    //添加，修改模块信息
    public function edit ($id) {

        $module = array();
        $style = array();

        //修改模块信息
        if ( $id >0) {
            $module = Module::find($id);
            if ( !$module) {
                return response()->json(['code' => 10000, 'message' => '模块信息没有找到']);
            }
            $style = ModuleStyle::where(['module_id'=>$id])->get()->toArray();
            if ( !$style ) {
                return response()->json([ 'code'=>10001, 'message'=>'样式库没有找到']);
            }
        }

        return view ('develop/moduleEdit',['module'=>$module,'id'=>$id,'style'=>$style]);
    }

    //保存模块信息
    public function store(Request $request)
    {
        $module_name = $request->input('module_name');
        $module_type = $request->input('module_type');
        $show_pic = $request->input('show_pic');
        $html = $request->input('html');
        $style_name = $request->input('style_name');
        $style_css = $request->input('style_css');
        $id = $request->input('id');

        if ( empty($module_name) ) {
            return response()->json(array('code' => 100001, 'message' => '模块名称不能为空'));
        }
        if ( empty($module_type) || $module_type == 0) {
            return response()->json(array('code' => 100002, 'message' => '模块类型不能为空'));
        }
        if ( empty($show_pic) ) {
            return response()->json(array('code' => 100003, 'message' => '展示图片不能为空'));
        }
        if ( empty($html) ) {
            return response()->json(array('code' => 100004, 'message' => 'HTML代码不能为空'));
        }
        if ( empty($style_name) ) {
            return response()->json(array('code' => 100005, 'message' => '默认样式库名称不能为空'));
        }
        if ( empty($style_css) ) {
            return response()->json(array('code' => 100006, 'message' => '默认样式库CSS不能为空'));
        }
        try {
            //开始事务
            DB::beginTransaction();

            //保存模块数据
            $module = Module::find($id);
            if (!$module) {
                $module = new Module();
                $module->uuid = makeUuid();
                $module->timeStamp = Carbon::now();
                $module->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
                $module->createTime = Carbon::now();
            }
            $module->module_name = $module_name;
            $module->module_type = $module_type;
            $module->show_pic = $show_pic;
            $module->html = $html;
            $module->save();

            //保存样式库数据
            $id = $module->id;
            $style = ModuleStyle::where(['module_id'=>$id])->first();
            if (!$style['attributes']['id']) {
                $style = new ModuleStyle();
                $style->uuid = makeUuid();
                $style->timeStamp = Carbon::now();
                $style->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
                $style->createTime = Carbon::now();
                $style->module_id = $id;
            }
            $style->style_name = $style_name;
            $style->style_css = $style_css;
            $style->save();

            //提交事务
            DB::commit();
        } catch (Exception $e) {
            //事务回滚
            DB::rollBack();
            return response()->json(array('code' => 100007, 'message' => '保存失败'));
        }
        return response()->json(['code' => 200, 'message' => '保存成功']);
    }

    //删除模块信息
    public function delete($id)
    {

        try {
            //开始事务
            DB::beginTransaction();

            $module = Module::find($id);
            if ( $module ) {
                $module->delete();
            }
            ModuleStyle::where(['module_id'=>$id])->delete();

            //提交事务
            DB::commit();

            } catch (Exception $e) {
            //事务回滚
            DB::rollBack();

            return response()->json(array('code' => 100001, 'message' => '删除失败'));
        }
        return response()->json(['code' => 200, 'message' => '删除成功']);

    }

}
